package BUS;

import DTO.*;
import DAO.*;

import java.util.*;

import android.content.Context;

/*
 * LÃ¡Â»â€ºp gÃ¡Â»â€œm nhÃ¡Â»Â¯ng phÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c phÃ¡Â»Â¥c vÃ¡Â»Â¥ cho
 * cÃƒÂ´ng viÃ¡Â»â€¡c tÃƒÂ¬m kiÃ¡ÂºÂ¿m 
 */
public class SearchEngine {

	private DichVuDAO _dichVuDAO = null;
	private DuLieuDAO _duLieuDAO = null;
	private DuongDAO _duongDAO = null;
	private PhuongDAO _phuongDAO = null;
	private QuanHuyenDAO _quanHuyenDAO = null;
	private TenDiaDiemDAO _tenDiaDiemDAO = null;
	private ChiTietDichVuDAO chiTietDichVuDAO = null;
	private ChiTietDuLieuDAO chiTietDuLieuDAO = null;
	private TinhThanhDAO _tinhThanhDAO = null;
	private TuKhoaDiaDiemDAO _tuKhoaDiaDiemDAO = null;
	private TuKhoaDichVuDAO _tuKhoaDichVuDAO = null;
	private TuKhoaDuongDAO _tuKhoaDuongDAO = null;
	private TuKhoaPhuongDAO _tuKhoaPhuongDAO = null;
	private TuKhoaQuanHuyenDAO _tuKhoaQuanHuyenDAO = null;
	private TuKhoaTinhThanhDAO _tuKhoaTinhThanhDAO = null;

	private List<SearchResultObject> _lstSearchResult = null;

	private DatabaseHelper helper;

	private List<TuKhoaDichVuDTO> _lstTuKhoaDichVuDTO;
	private List<TuKhoaDuongDTO> _lstTuKhoaDuongDTO;
	private List<TuKhoaPhuongDTO> _lstTuKhoaPhuongDTO;
	private List<TuKhoaQuanHuyenDTO> _lstTuKhoaQuanHuyenDTO;

	private ArrayList<DuLieuDTO> _resultQueue;

	// HÃƒÂ m dÃ¡Â»Â±ng

	public SearchEngine(Context contex) throws Exception {
		try {
			helper = new DatabaseHelper(contex, "data", null, 1);
			helper.checkAndCopyDatabase();

			helper.open();
			this._dichVuDAO = new DichVuDAO(null, null, null, 1);
			this._duLieuDAO = new DuLieuDAO(null, null, null, 1);
			this._duongDAO = new DuongDAO(null, null, null, 1);
			this._phuongDAO = new PhuongDAO(null, null, null, 1);
			this._quanHuyenDAO = new QuanHuyenDAO(null, null, null, 1);
			this._tenDiaDiemDAO = new TenDiaDiemDAO(null, null, null, 1);
			this._tinhThanhDAO = new TinhThanhDAO(null, null, null, 1);
			this._tuKhoaDiaDiemDAO = new TuKhoaDiaDiemDAO(null, null, null, 1);
			this._tuKhoaDichVuDAO = new TuKhoaDichVuDAO(null, null, null, 1);
			this._tuKhoaDuongDAO = new TuKhoaDuongDAO(null, null, null, 1);
			this._tuKhoaPhuongDAO = new TuKhoaPhuongDAO(null, null, null, 1);
			this._tuKhoaQuanHuyenDAO = new TuKhoaQuanHuyenDAO(null, null, null,
					1);
			this._tuKhoaTinhThanhDAO = new TuKhoaTinhThanhDAO(null, null, null,
					1);
			this.chiTietDichVuDAO = new ChiTietDichVuDAO(null, null, null, 1);
			this.chiTietDuLieuDAO = new ChiTietDuLieuDAO(null, null, null, 1);

			this._lstSearchResult = new ArrayList<SearchResultObject>();
			_resultQueue = new ArrayList<DuLieuDTO>();

			_lstTuKhoaDichVuDTO = TuKhoaDichVuDAO.getAll();

			_lstTuKhoaDuongDTO = TuKhoaDuongDAO.getAll();

			_lstTuKhoaPhuongDTO = TuKhoaPhuongDAO.getAll();

			_lstTuKhoaQuanHuyenDTO = TuKhoaQuanHuyenDAO.getAll();

		} catch (Exception ex) {
			throw ex;
		} finally {
			helper.close();
		}
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c lÃ¡ÂºÂ¥y danh sÃƒÂ¡ch tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c
	 * dÃ¡Â»â€¹ch vÃ¡Â»Â¥ tÃ¡Â»Â« CSDL thÃƒÂ´ng qua lÃ¡Â»â€ºp DichVuDTO
	 * 
	 * @param: khÃƒÂ´ng cÃƒÂ³
	 * 
	 * @return: 1 ArrayList chÃ¡Â»Â©c tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« hÃƒÂ m GetAllDichVuDTO() cÃ¡Â»Â§a
	 * lÃ¡Â»â€ºp DichVuDTO *
	 * 
	 * @exception
	 */
	public ArrayList<DichVuDTO> GetAllDichVuDTO() throws Exception {
		ArrayList<DichVuDTO> lstDichVuDTO = null;
		try {
			lstDichVuDTO = this._dichVuDAO.GetAllDichVuDTO();
		} catch (Exception ex) {
			lstDichVuDTO = new ArrayList<DichVuDTO>();
			throw ex;
		} finally {
		}
		return lstDichVuDTO;
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c lÃ¡ÂºÂ¥y danh sÃƒÂ¡ch tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c
	 * quÃ¡ÂºÂ­n/huyÃ¡Â»â€¡n tÃ¡Â»Â« CSDL thÃƒÂ´ng qua lÃ¡Â»â€ºp QuanHuyenDTO
	 * 
	 * @param: khÃƒÂ´ng cÃƒÂ³
	 * 
	 * @return: 1 ArrayList chÃ¡Â»Â©c tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c
	 * quÃ¡ÂºÂ­n/huyÃ¡Â»â€¡n Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« hÃƒÂ m
	 * GetAllQuanHuyenDTO() cÃ¡Â»Â§a lÃ¡Â»â€ºp QuanHuyenDTO
	 * 
	 * @exception
	 */
	public ArrayList<QuanHuyenDTO> GetAllQuanHuyenDTO() {
		ArrayList<QuanHuyenDTO> lstQuanHuyenDTO = null;
		try {
			lstQuanHuyenDTO = this._quanHuyenDAO.GetAllQuanHuyenDTO();
		} catch (Exception ex) {
			lstQuanHuyenDTO = new ArrayList<QuanHuyenDTO>();
			try {
				throw ex;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lstQuanHuyenDTO;
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c lÃ¡ÂºÂ¥y danh sÃƒÂ¡ch tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c
	 * phÃ†Â°Ã¡Â»Âng tÃ¡Â»Â« CSDL thÃƒÂ´ng qua lÃ¡Â»â€ºp PhuongDTO
	 * 
	 * @param: khÃƒÂ´ng cÃƒÂ³
	 * 
	 * @return: 1 ArrayList chÃ¡Â»Â©c tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c phÃ†Â°Ã¡Â»Âng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« hÃƒÂ m GetAllPhuongDTO() cÃ¡Â»Â§a
	 * lÃ¡Â»â€ºp PhuongDTO
	 * 
	 * @exception
	 */
	public ArrayList<PhuongDTO> GetAllPhuongDTO() {
		ArrayList<PhuongDTO> lstPhuongDTO = null;
		try {
			lstPhuongDTO = this._phuongDAO.GetAllPhuongDTO();
		} catch (Exception ex) {
			lstPhuongDTO = new ArrayList<PhuongDTO>();
			try {
				throw ex;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lstPhuongDTO;
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c lÃ¡ÂºÂ¥y danh sÃƒÂ¡ch tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c
	 * Ã„â€˜Ã†Â°Ã¡Â»Âng tÃ¡Â»Â« CSDL thÃƒÂ´ng qua lÃ¡Â»â€ºp DuongDTO
	 * 
	 * @param: khÃƒÂ´ng cÃƒÂ³
	 * 
	 * @return: 1 ArrayList chÃ¡Â»Â©c tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c Ã„â€˜Ã†Â°Ã¡Â»Âng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« hÃƒÂ m GetAllDuongDTO() cÃ¡Â»Â§a
	 * lÃ¡Â»â€ºp DuongDTO
	 * 
	 * @exception
	 */
	public ArrayList<DuongDTO> GetAllDuongDTO() {
		ArrayList<DuongDTO> lstDuongDTO = null;
		try {
			// helper.open();
			lstDuongDTO = this._duongDAO.GetAllDuongDTO();
		} catch (Exception ex) {
			lstDuongDTO = new ArrayList<DuongDTO>();
			try {
				throw ex;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lstDuongDTO;
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c lÃ¡ÂºÂ¥y danh sÃƒÂ¡ch tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ kÃ¡ÂºÂ¿t
	 * quÃ¡ÂºÂ£ tÃƒÂ¬m kiÃ¡ÂºÂ¿m Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« cÃƒÂ¡c
	 * phÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c tÃƒÂ¬m kiÃ¡ÂºÂ¿m nhÃ¡Â»Â hÃ†Â¡n khÃƒÂ¡c
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m do ngÃ†Â°Ã¡Â»Âi dÃƒÂ¹ng nhÃ¡ÂºÂ­p
	 * vÃƒÂ o
	 * 
	 * @param: dÃ¡Â»â€¹ch vÃ¡Â»Â¥ Ã†Â°u tiÃƒÂªn
	 * 
	 * @return: 1 ArrayList chÃ¡Â»Â©c tÃ¡ÂºÂ¥t cÃ¡ÂºÂ£ cÃƒÂ¡c kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£
	 * tÃƒÂ¬m kiÃ¡ÂºÂ¿m Ã„â€˜Ã†Â°Ã¡Â»Â£c lÃ¡ÂºÂ¥y tÃ¡Â»Â« cÃƒÂ¡c phÃ†Â°Ã†Â¡ng
	 * thÃ¡Â»Â©c tÃƒÂ¬m kiÃ¡ÂºÂ¿m nhÃ¡Â»Â hÃ†Â¡n khÃƒÂ¡c
	 * 
	 * @exception
	 */
	public ArrayList<DuLieuDTO> Search(String strInput, int maDichVu, int index) {
		ArrayList<DuLieuDTO> lstResult = new ArrayList<DuLieuDTO>();
		try {

			if (null != this._lstSearchResult) {
				this._lstSearchResult.clear();
			} else {
				this._lstSearchResult = new ArrayList<SearchResultObject>();
			}

			helper.open();
			if (strInput.length() > 0) {
				boolean isExistNumber = false;
				if (isExistNumber) {

					ArrayList<DuLieuDTO> lstDuLieuDTO = this._duLieuDAO
							.searchWithSoNha(strInput);

					// lstDuLieuDTO = lstDuLieuDTO.RemoveDupicatesOnSoNha();

					int nDuLieuDTO = lstDuLieuDTO.size();
					if (nDuLieuDTO > 0) {
						String strTemp = "";
						SearchResultObject sr;
						for (int i = 0; i < nDuLieuDTO; i++) {
							strTemp = Clear(strInput, lstDuLieuDTO.get(i)
									.SoNha());
							if (strTemp.length() > 0) {

								this.searchTuKhoaDichVu(strTemp, lstDuLieuDTO
										.get(i).SoNha(), maDichVu);
							} else {
								sr = new SearchResultObject();
								sr.SoNha(lstDuLieuDTO.get(i).SoNha());
								if (0 != maDichVu) {
									sr.MaDichVu(maDichVu);
								}

								this._lstSearchResult.add(sr);
							}
						}
					}

					this.searchTuKhoaDichVu(strInput, "", maDichVu);
				} else {
					this.searchTuKhoaDichVu(strInput, "", maDichVu);
				}

				List<DuLieuDTO> lstTemp = null;
				int nSearchResult = this._lstSearchResult.size();
				for (int i = 0; i < nSearchResult; i++) {
					lstTemp = this._duLieuDAO.Search(
							this._lstSearchResult.get(i).SoNha(),
							this._lstSearchResult.get(i).MaDichVu(),
							this._lstSearchResult.get(i).MaTinhThanh(),
							this._lstSearchResult.get(i).MaQuanHuyen(),
							this._lstSearchResult.get(i).MaPhuong(),
							this._lstSearchResult.get(i).MaDuong(),
							this._lstSearchResult.get(i).MaTenDiaDiem(), index);

					lstResult.addAll(lstTemp);
				}
			} else {
				if (0 != maDichVu) {
					this.searchTuKhoaDichVu(strInput, "", maDichVu);
					List<DuLieuDTO> lstTemp = null;
					int nSearchResult = this._lstSearchResult.size();
					for (int i = 0; i < nSearchResult; i++) {
						lstTemp = this._duLieuDAO.Search(this._lstSearchResult
								.get(i).SoNha(), this._lstSearchResult.get(i)
								.MaDichVu(), this._lstSearchResult.get(i)
								.MaTinhThanh(), this._lstSearchResult.get(i)
								.MaQuanHuyen(), this._lstSearchResult.get(i)
								.MaPhuong(), this._lstSearchResult.get(i)
								.MaDuong(), this._lstSearchResult.get(i)
								.MaTenDiaDiem(), index);
						lstResult.add((DuLieuDTO) lstTemp);
					}
				} else {
					lstResult = new ArrayList<DuLieuDTO>();
				}
			}
		} catch (Exception ex) {
			lstResult = new ArrayList<DuLieuDTO>();
			try {
				throw ex;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} finally {
			helper.close();
		}
		return lstResult;
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a dÃ¡Â»â€¹ch vÃ¡Â»Â¥, khÃƒÂ´ng cÃƒÂ³ kÃ¡ÂºÂ¿t
	 * quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i cÃƒÂ¹ng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n toÃƒÂ n
	 * cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaDichVu(String strInput, String soNha, int maDichVu) {
		if (0 == maDichVu) {
			List<TuKhoaDichVuDTO> lstTuKhoaDichVuDTO = new ArrayList<TuKhoaDichVuDTO>();

			for (TuKhoaDichVuDTO tuKhoaDichVuDTO : _lstTuKhoaDichVuDTO) {
				if (strInput.indexOf(tuKhoaDichVuDTO.TuKhoaDichVu()) > -1) {
					lstTuKhoaDichVuDTO.add(tuKhoaDichVuDTO);
				}
			}

			int nTuKhoaDichVuDTO = lstTuKhoaDichVuDTO.size();
			if (nTuKhoaDichVuDTO > 0) {
				String strTemp = null;
				for (int i = 0; i < nTuKhoaDichVuDTO; i++) {
					strTemp = Clear(strInput, lstTuKhoaDichVuDTO.get(i)
							.TuKhoaDichVu());
					if (strTemp.length() > 0) {
						this.searchTuKhoaTinhThanh(strTemp, soNha,
								lstTuKhoaDichVuDTO.get(i).MaDichVu());
					} else {
						SearchResultObject sr = new SearchResultObject();
						sr.SoNha(soNha);
						sr.MaDichVu(lstTuKhoaDichVuDTO.get(i).MaDichVu());
						this._lstSearchResult.add(sr);
					}
				}
			}

			this.searchTuKhoaTinhThanh(strInput, soNha,
					DatabaseHelper._defaultNumber);
		} else {
			if (0 == strInput.length()) {
				SearchResultObject sr = new SearchResultObject();
				sr.SoNha(soNha);
				sr.MaDichVu(maDichVu);
				this._lstSearchResult.add(sr);
			} else {
				this.searchTuKhoaTinhThanh(strInput, soNha, maDichVu);
			}
		}
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a tÃ¡Â»â€°nh thÃƒÂ nh, khÃƒÂ´ng cÃƒÂ³ kÃ¡ÂºÂ¿t
	 * quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i cÃƒÂ¹ng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n toÃƒÂ n
	 * cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: sÃ¡Â»â€˜ nhÃƒÂ 
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaTinhThanh(String strInput, String soNha,
			int maDichVu) {
		List<TuKhoaTinhThanhDTO> lstTuKhoaTinhThanhDTO = this._tuKhoaTinhThanhDAO
				.Search(strInput);

		int nTuKhoaTinhThanhDTO = lstTuKhoaTinhThanhDTO.size();
		if (nTuKhoaTinhThanhDTO > 0) {
			String strTemp = null;
			for (int i = 0; i < lstTuKhoaTinhThanhDTO.size(); i++) {
				strTemp = Clear(strInput, lstTuKhoaTinhThanhDTO.get(i)
						.TuKhoaTinhThanh());
				if (strTemp.length() > 0) {
					this.searchTuKhoaQuanHuyen(strTemp, soNha, maDichVu,
							lstTuKhoaTinhThanhDTO.get(i).MaTinhThanh());
				} else {
					SearchResultObject sr = new SearchResultObject();
					sr.SoNha(soNha);
					sr.MaDichVu(maDichVu);
					sr.MaTinhThanh(lstTuKhoaTinhThanhDTO.get(i).MaTinhThanh());
					this._lstSearchResult.add(sr);
				}
			}
		}

		this.searchTuKhoaQuanHuyen(strInput, soNha, maDichVu,
				DatabaseHelper._defaultNumber);
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a quÃ¡ÂºÂ­n/huyÃ¡Â»â€¡n, khÃƒÂ´ng cÃƒÂ³
	 * kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i
	 * cÃƒÂ¹ng Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n
	 * toÃƒÂ n cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: sÃ¡Â»â€˜ nhÃƒÂ 
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @param: mÃƒÂ£ tÃ¡Â»â€°nh thÃƒÂ nh
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaQuanHuyen(String strInput, String soNha,
			int maDichVu, int maTinhThanh) {

		List<TuKhoaQuanHuyenDTO> lstTuKhoaQuanHuyenDTO = new ArrayList<TuKhoaQuanHuyenDTO>();
		for (TuKhoaQuanHuyenDTO tuKhoaQuanHuyenDTO : _lstTuKhoaQuanHuyenDTO) {
			if (strInput.indexOf(tuKhoaQuanHuyenDTO.TuKhoaQuanHuyen()) > -1) {
				lstTuKhoaQuanHuyenDTO.add(tuKhoaQuanHuyenDTO);
			}
		}

		int nTuKhoaQuanHuyenDTO = lstTuKhoaQuanHuyenDTO.size();
		if (nTuKhoaQuanHuyenDTO > 0) {
			String strTemp = null;
			for (int i = 0; i < lstTuKhoaQuanHuyenDTO.size(); i++) {
				strTemp = Clear(strInput, lstTuKhoaQuanHuyenDTO.get(i)
						.TuKhoaQuanHuyen());
				if (strTemp.length() > 0) {
					this.searchTuKhoaPhuong(strTemp, soNha, maDichVu,
							maTinhThanh, lstTuKhoaQuanHuyenDTO.get(i)
									.MaQuanHuyen());
				} else {
					SearchResultObject sr = new SearchResultObject();
					sr.SoNha(soNha);
					sr.MaDichVu(maDichVu);
					sr.MaTinhThanh(maTinhThanh);
					sr.MaQuanHuyen(lstTuKhoaQuanHuyenDTO.get(i).MaQuanHuyen());
					this._lstSearchResult.add(sr);
				}
			}
		}

		this.searchTuKhoaPhuong(strInput, soNha, maDichVu, maTinhThanh,
				DatabaseHelper._defaultNumber);
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a phÃ†Â°Ã¡Â»Âng , khÃƒÂ´ng cÃƒÂ³ kÃ¡ÂºÂ¿t
	 * quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i cÃƒÂ¹ng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n toÃƒÂ n
	 * cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: sÃ¡Â»â€˜ nhÃƒÂ 
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @param: mÃƒÂ£ tÃ¡Â»â€°nh thÃƒÂ nh
	 * 
	 * @param: mÃƒÂ£ quÃ¡ÂºÂ­n huyÃ¡Â»â€¡n
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaPhuong(String strInput, String soNha,
			int maDichVu, int maTinhThanh, int maQuanHuyen) {

		List<TuKhoaPhuongDTO> lstTuKhoaPhuongDTO = new ArrayList<TuKhoaPhuongDTO>();

		for (TuKhoaPhuongDTO tuKhoaPhuongDTO : _lstTuKhoaPhuongDTO) {
			if (strInput.indexOf(tuKhoaPhuongDTO.TuKhoaPhuong()) > -1) {
				lstTuKhoaPhuongDTO.add(tuKhoaPhuongDTO);
			}
		}

		int nTuKhoaPhuongDTO = lstTuKhoaPhuongDTO.size();
		if (nTuKhoaPhuongDTO > 0) {
			String strTemp = null;
			for (int i = 0; i < lstTuKhoaPhuongDTO.size(); i++) {
				strTemp = Clear(strInput, lstTuKhoaPhuongDTO.get(i)
						.TuKhoaPhuong());
				if (strTemp.length() > 0) {
					this.searchTuKhoaDuong(strTemp, soNha, maDichVu,
							maTinhThanh, maQuanHuyen, lstTuKhoaPhuongDTO.get(i)
									.MaPhuong());
				} else {
					SearchResultObject sr = new SearchResultObject();
					sr.SoNha(soNha);
					sr.MaDichVu(maDichVu);
					sr.MaTinhThanh(maTinhThanh);
					sr.MaQuanHuyen(maQuanHuyen);
					sr.MaPhuong(lstTuKhoaPhuongDTO.get(i).MaPhuong());
					this._lstSearchResult.add(sr);
				}
			}
		}

		this.searchTuKhoaDuong(strInput, soNha, maDichVu, maTinhThanh,
				maQuanHuyen, DatabaseHelper._defaultNumber);
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a Ã„â€˜Ã†Â°Ã¡Â»Âng, khÃƒÂ´ng cÃƒÂ³ kÃ¡ÂºÂ¿t
	 * quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i cÃƒÂ¹ng
	 * Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n toÃƒÂ n
	 * cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: sÃ¡Â»â€˜ nhÃƒÂ 
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @param: mÃƒÂ£ tÃ¡Â»â€°nh thÃƒÂ nh
	 * 
	 * @param: mÃƒÂ£ quÃ¡ÂºÂ­n huyÃ¡Â»â€¡n
	 * 
	 * @param: mÃƒÂ£ phÃ†Â°Ã¡Â»Âng
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaDuong(String strInput, String soNha, int maDichVu,
			int maTinhThanh, int maQuanHuyen, int maPhuong) {
		/*
		 * ArrayList<TuKhoaDuongDTO> lstTuKhoaDuongDTO = this._tuKhoaDuongDAO
		 * .Search(strInput);
		 */

		List<TuKhoaDuongDTO> lstTuKhoaDuongDTO = new ArrayList<TuKhoaDuongDTO>();
		// for(int i=0;i<_lstTuKhoaDuongDTO.size();i++)
		int n = _lstTuKhoaDuongDTO.size();
		for (TuKhoaDuongDTO tuKhoaDuongDTO : _lstTuKhoaDuongDTO) {

			/*
			 * if(strInput.indexOf(tuKhoaDuongDTO.TuKhoaDuong()) > -1) {
			 * lstTuKhoaDuongDTO.add(tuKhoaDuongDTO); }
			 */
			if (strInput.equals(tuKhoaDuongDTO.TuKhoaDuong())) {
				lstTuKhoaDuongDTO.add(tuKhoaDuongDTO);
			}

		}

		int nTuKhoaDuongDTO = lstTuKhoaDuongDTO.size();
		if (nTuKhoaDuongDTO > 0) {
			String strTemp = null;
			for (int i = 0; i < lstTuKhoaDuongDTO.size(); i++) {
				strTemp = Clear(strInput, lstTuKhoaDuongDTO.get(i)
						.TuKhoaDuong());
				if (strTemp.length() > 0) {
					this.searchTuKhoaDiaDiem(strTemp, soNha, maDichVu,
							maTinhThanh, maQuanHuyen, maPhuong,
							lstTuKhoaDuongDTO.get(i).MaDuong());
				} else {
					SearchResultObject sr = new SearchResultObject();
					sr.SoNha(soNha);
					sr.MaDichVu(maDichVu);
					sr.MaTinhThanh(maTinhThanh);
					sr.MaQuanHuyen(maQuanHuyen);
					sr.MaPhuong(maPhuong);
					sr.MaDuong(lstTuKhoaDuongDTO.get(i).MaDuong());
					this._lstSearchResult.add(sr);
				}
			}
		}

		this.searchTuKhoaDiaDiem(strInput, soNha, maDichVu, maTinhThanh,
				maQuanHuyen, maPhuong, DatabaseHelper._defaultNumber);
	}

	/*
	 * PhÃ†Â°Ã†Â¡ng thÃ¡Â»Â©c thÃ¡Â»Â±c hiÃ¡Â»â€¡n tÃƒÂ¬m kiÃ¡ÂºÂ¿m tÃ¡ÂºÂ­p
	 * trung vÃƒÂ o tÃ¡Â»Â« khÃƒÂ³a Ã„â€˜Ã¡Â»â€¹a Ã„â€˜iÃ¡Â»Æ’m, khÃƒÂ´ng cÃƒÂ³
	 * kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ trÃ¡ÂºÂ£ vÃ¡Â»Â nhÃ†Â°ng kÃ¡ÂºÂ¿t quÃ¡ÂºÂ£ cuÃ¡Â»â€˜i
	 * cÃƒÂ¹ng Ã„â€˜Ã†Â°Ã¡Â»Â£c cÃ¡ÂºÂ­p nhÃ¡ÂºÂ­t thÃƒÂ´ng qua biÃ¡ÂºÂ¿n
	 * toÃƒÂ n cÃ¡Â»Â¥c _lstSearchResult
	 * 
	 * @param: chuÃ¡Â»â€”i tÃƒÂ¬m kiÃ¡ÂºÂ¿m sau khi cÃ¡ÂºÂ¯t bÃ¡Â»â€ºt
	 * 
	 * @param: sÃ¡Â»â€˜ nhÃƒÂ 
	 * 
	 * @param: mÃƒÂ£ dÃ¡Â»â€¹ch vÃ¡Â»Â¥
	 * 
	 * @param: mÃƒÂ£ tÃ¡Â»â€°nh thÃƒÂ nh
	 * 
	 * @param: mÃƒÂ£ quÃ¡ÂºÂ­n huyÃ¡Â»â€¡n
	 * 
	 * @param: mÃƒÂ£ phÃ†Â°Ã¡Â»Âng
	 * 
	 * @param: mÃƒÂ£ Ã„â€˜Ã†Â°Ã¡Â»Âng
	 * 
	 * @return: khÃƒÂ´ng
	 * 
	 * @exception
	 */
	private void searchTuKhoaDiaDiem(String strInput, String soNha,
			int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong,
			int maDuong) {
		/*
		 * ArrayList<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiemDTO =
		 * this._tuKhoaDiaDiemDAO .Search(strInput);
		 */
		ArrayList<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiemDTO = new ArrayList<TuKhoaDiaDiemDTO>();
		int nTuKhoaDiaDiemDTO = lstTuKhoaDiaDiemDTO.size();
		if (nTuKhoaDiaDiemDTO > 0) {
			String strTemp = null;
			for (int i = 0; i < lstTuKhoaDiaDiemDTO.size(); i++) {
				strTemp = Clear(strInput, lstTuKhoaDiaDiemDTO.get(i)
						.TuKhoaTenDiaDiem());
				if (0 == strTemp.length()) {
					SearchResultObject sr = new SearchResultObject();
					sr.SoNha(soNha);
					sr.MaDichVu(maDichVu);
					sr.MaTinhThanh(maTinhThanh);
					sr.MaQuanHuyen(maQuanHuyen);
					sr.MaPhuong(maPhuong);
					sr.MaDuong(maDuong);
					sr.MaTenDiaDiem(lstTuKhoaDiaDiemDTO.get(i).MaTenDiaDiem());
					this._lstSearchResult.add(sr);
				}
			}
		}
	}

	private static String Clear(String strInput, String strClear) {
		String strOutput = strInput;
		if (null != strClear && strClear.length() > 0) {
			int idx = strClear.indexOf(strInput);

			if (idx >= 0) {
				strOutput = "";
			} else {
				strOutput = strInput.replace(strClear, "");
				strOutput = strOutput.trim();
			}
		}

		return strOutput;
	}

	public boolean popResultList() {
		return _resultQueue.isEmpty();
	}

	public DuLieuThoDTO getDuLieu(int maDuLieu, int maDichVu, int idDiaDiem,
			String soNha, int idDuong, int idPhuong, int idQuanHuyen,
			Double kinhDo, Double viDo) {
		DuLieuThoDTO result = new DuLieuThoDTO();
		helper.open();

		result.MaDuLieu(maDuLieu);
		result.MaDichVu(maDichVu);

		TenDiaDiemDTO tenDiaDiem = _tenDiaDiemDAO.Search(idDiaDiem);
		if (tenDiaDiem != null)
			result.TenDiaDiem(tenDiaDiem.TenDiaDiem());

		result.SoNha(soNha);

		DuongDTO tenDuong = _duongDAO.Search(idDuong);
		if (tenDuong != null)
			result.TenDuong(tenDuong.TenDuong());

		PhuongDTO tenPhuong = _phuongDAO.Search(idPhuong);
		if (tenPhuong != null)
			result.TenPhuong(tenPhuong.TenPhuong());

		QuanHuyenDTO tenQuanHuyen = _quanHuyenDAO.Search(idQuanHuyen);
		if (tenQuanHuyen != null)
			result.TenQuanHuyen(tenQuanHuyen.TenQuanHuyen());

		result.KinhDo(kinhDo);
		result.ViDo(viDo);

		helper.close();

		return result;
	}

	public ArrayList<DuLieuDTO> getdulieu() {
		return _resultQueue;
	}

	public ArrayList<DuLieuDTO> FastSearch(int index) {
		helper.open();
		ArrayList<DuLieuDTO> lstResult = new ArrayList<DuLieuDTO>();
		List<DuLieuDTO> lstTemp = null;
		int nSearchResult = this._lstSearchResult.size();
		for (int i = 0; i < nSearchResult; i++) {
			lstTemp = this._duLieuDAO.Search(this._lstSearchResult.get(i)
					.SoNha(), this._lstSearchResult.get(i).MaDichVu(),
					this._lstSearchResult.get(i).MaTinhThanh(),
					this._lstSearchResult.get(i).MaQuanHuyen(),
					this._lstSearchResult.get(i).MaPhuong(),
					this._lstSearchResult.get(i).MaDuong(),
					this._lstSearchResult.get(i).MaTenDiaDiem(), index);
			lstResult.addAll(lstTemp);
		}
		helper.close();
		return lstResult;
	}

	public ArrayList<ChiTietDichVuDTO> SearchChiTietDV(int maDuLieu) {
		ArrayList<ChiTietDichVuDTO> result = new ArrayList<ChiTietDichVuDTO>();
		helper.open();

		int listMaChitiet[] = this.chiTietDuLieuDAO.Search(maDuLieu);
		for (int i : listMaChitiet) {
			ChiTietDichVuDTO chiTietDV = this.chiTietDichVuDAO.Search(i);
			result.add(chiTietDV);
		}

		helper.close();
		return result;
	}
}
